package xyz.vmcserver.wdibbs.model.service.impl;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.stereotype.Service;
import xyz.vmcserver.wdibbs.model.dao.UserGroupMapper;
import xyz.vmcserver.wdibbs.model.po.UserGroup;
import xyz.vmcserver.wdibbs.model.service.UserGroupService;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Service("UserGroupService")
public class UserGroupServiceImpl implements UserGroupService {
    @Override
    public UserGroup selectAllByPage(Integer page, Integer startPage) {
        return null;
    }

    @Override
    public List<UserGroup> selectAll() {
        try {
            InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
            SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
            SqlSession sqlSession = build.openSession();
            UserGroupMapper mapper = sqlSession.getMapper(UserGroupMapper.class);
            List<UserGroup> userGroupList= mapper.selectAll();
            return userGroupList;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
//        return null;
    }

    @Override
    public UserGroup selectAllByGroupAlias(String group_alias) {
        try {
            InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
            SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
            SqlSession sqlSession = build.openSession();
            UserGroupMapper mapper = sqlSession.getMapper(UserGroupMapper.class);
            UserGroup userGroup = mapper.selectGroupInfoByGroupAlias(group_alias);
            return userGroup;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


}
